/**
 * This file is part of the educational usermanagement system.
 * 
 * Copyright (c) 2008, Florian Arndt, Thomas Kemptner, Christian Kerl, Patrick Neuberger, Marcel Rehfeld, Emal Sadran, Stefan Sauer
 * 
 * Released under the MIT License http://www.opensource.org/licenses/mit-license.php
 */

package edu.ba.management;

import java.util.*;

/**
 * The Group class represents a group in the management system.
 */
public class Group implements Cloneable
{
    public static final int INVALID_ID = -1;

    private int id = INVALID_ID;
    private String name;
    private ArrayList<Integer> users = new ArrayList<Integer>();

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#clone()
     */
    @Override
    public Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }

    /**
     * Gets the id.
     * 
     * @return the id
     */
    public int getId()
    {
        return id;
    }

    /**
     * Sets the id.
     * 
     * @param id the id to set
     */
    void setId(int id)
    {
        this.id = id;
    }

    /**
     * Gets the name.
     * 
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the name.
     * 
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * Gets the users, which are member of the group.
     * 
     * @return the users
     */
    public List<Integer> getUsers()
    {
        return users;
    }

    /**
     * Checks whether or not the given user is member of the group.
     * 
     * @param user the user to check against
     * @return true, if the group contains the user, false otherwise
     */
    public boolean hasUser(User user)
    {
        return user != null && this.hasUser(user.getId());
    }

    /**
     * Checks whether or not the user with the given id is member of the group.
     * 
     * @param user the user id to check against
     * @return true, if the group contains the user, false otherwise
     */
    public boolean hasUser(int user)
    {
        return this.users.contains(user);
    }

    /**
     * Adds the given user to the group.
     * 
     * @param user the user, which should be added to the group
     * @return false, when an error occurred, true otherwise
     */
    public boolean addUser(User user)
    {
        if (user != null && user.getId() != User.INVALID_ID
                && !this.hasUser(user.getId()) && this.getId() != INVALID_ID)
        {
            this.users.add(user.getId());
            user.addGroup(this);

            return true;
        }

        return false;
    }

    /**
     * Removes the given user from the group.
     * 
     * @param user the user, which should be removed from the group
     * @return false, when an error occurred, true otherwise
     */
    public boolean removeUser(User user)
    {
        if (user != null && user.getId() != User.INVALID_ID
                && this.hasUser(user.getId()))
        {
            this.users.remove((Object) user.getId());
            user.removeGroup(this);

            return true;
        }

        return false;
    }
}
